<?php
require_once 'Feedback.php';
require_once 'Validate.php';
class RegisterHandler {
	public function doRegister($username, $password) {
		if ($username && $password !== null) {
			$dbc = new DBConnection();
			$dbc -> connect();
			$userDAL = new UserDAL($dbc);
			// Check if username is avaliable
			if (!$userDAL -> userExists($username)) {
				if ($userDAL -> addUser($username, $password) !== false) {
					// If registration was successful, return true. 
					return true;
				} else {
					// If registration failed, add errormessage
					Feedback::addError(Feedback::failedRegistration);
				}
			} else {
				// If username already exists, add errormessage
				Feedback::addError(Feedback::userExists);
			}
		}
	}

	public function deleteUser($username) {
		$dbc = new DBConnection();
		$dbc -> connect();
		$userDAL = new UserDAL($dbc);
		if ($ret = $userDAL -> deleteUser($username)) {
			Feedback::addFeedback(Feedback::userDeleted);
		} else {
			Feedback::addError(Feedback::userDeleteFail);
		}
		return $ret;
	}

}
